#include "string_util.h"
#include "inc.h"
USING_NS_STD;
USING_NS_TEST;

vector<string> StringUtil::split(const string& content, char delim) {
    vector<string> result;
    string::size_type last = 0;
    string::size_type index = content.find_first_of(delim, last);
    while (index != string::npos) {
        result.push_back(content.substr(last, index - last));
        last = index + 1;
        while (last < content.size() && content[last] == ' ') {
            ++last;
        }
        index = content.find_first_of(delim, last);
    }

    if (last < content.size()) {
        result.push_back(content.substr(last));
    }
    return result;
}

vector<wstring> StringUtil::split(const wstring& content, wchar_t delim) {
    vector<wstring> result;
    wstring::size_type last = 0;
    wstring::size_type index = content.find_first_of(delim, last);
    while (index != wstring::npos) {
        result.push_back(content.substr(last, index - last));
        last = index + 1;
        while (last < content.size() && content[last] == L' ') {
            ++last;
        }
        index = content.find_first_of(delim, last);
    }

    if (last < content.size()) {
        result.push_back(content.substr(last));
    }
    return result;
}


// void elloop_test::StringUtil::testSplit() {
//     StringUtil util;
//     string test("window 320 540 console no")
//     vector<string> t = util.split(test, ' ');
// //     for (string s : t) {
// //         psln(s);
// //     }
//     auto iter = t.begin();
//     while (iter != t.end()) {
//         psln(*iter);
//         ++iter;
//     }
// }
//
